<?php
$this->jQuery()->enable()->uiEnable();
$this->headLink()
        ->appendStylesheet($this->baseUrl('/lib/flexigrid-1.1/css/flexigrid.css'))
        ->appendStylesheet($this->baseUrl('/lib/flexigrid-1.1/css/flexigrid-custom.css'));
$this->headScript()
        ->appendFile($this->baseUrl('/lib/flexigrid-1.1/js/flexigrid.js'));
$this->pagetitle = 'Lịch sử các lần nâng cấp';
$uRole = Zend_Auth::getInstance()->getIdentity()->Role;
?>

<table class='gridTable' style="display:none"></table>

<div id="dialog-form" style="display: none">
    <p class="validateTips">All form fields are required.</p>
    <?php echo $this->form; ?>
</div>


<div id="dialog-confirm" title="Delete confirm?" style="display: none">
    <p>
        <span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>
        Information of item will be permanently deleted and cannot be recovered. Are you sure?
    </p>
</div>

<script type="text/javascript">
    <!--
    $(document).ready(function(){
        
        $('.gridTable').flexigrid({
            url : '<?php echo $this->url(array('module' => 'asset', 'controller' => 'upgrade', 'action' => 'records')); ?>',
            dataType : 'json',
            colModel : [ 
                {display: 'Mã TS', name: 'MaTS', width: 70, sortable: true, align: 'left'},
                {display: 'Tên tài sản', name: 'TenTS', width: 200, sortable: true, align: 'left'},
                {display: 'Người sử dụng', name: 'Username', width: 100, sortable: true, align: 'left'},
                {display: 'Người nâng cấp', name: 'Manager', width: 100, sortable: true, align: 'left'},
                {display: 'Ngày nâng cấp', name: 'Date', width: 70, sortable: true, align: 'left'},
                {display: 'Chi tiết nâng cấp', name: 'Detail', width: 300, sortable: false, align: 'left'},
            ],
            buttons : [ 
<?php if ($uRole == 0 || $uRole == 2) : ?>
                    {name : 'Add', bclass : 'add', onpress : add_record},
                    {name : 'Delete', bclass : 'delete', onpress : delete_record}, 
<?php endif; ?>
                {separator : true},
                {name : 'Detail', bclass : 'detail', onpress : detail_record}
            ],
            searchitems : [ 
                {display : 'Mã TS', name : 'Ma_tai_san', isdefault : true },
                {display : 'Tên tài sản', name : 'Ten_tai_san' },
                {display : 'Người sử dụng', name : 'Username' },
                {display : 'Người nâng cấp', name : 'Manager' },
                {display : 'Ngày nâng cấp', name : 'Date' }
            ],
            sortname : "UpgradeID",
            sortorder : "asc",
            usepager : true,
            title : 'Bảng lịch sử nâng cấp',
            useRp : true,
            rp : 15,
            singleSelect : true,
            showTableToggleBtn : false,
            width : 1080,
            height : 250
        });
        
        function detail_record(command,grid){
            var record_count = $('.trSelected',grid).length;
            if (0 == record_count)
            {
                alert('Please select a record first by clicking on the appropriate row.');
                return;
            }
            $('.trSelected',grid).each(function(){
                var id = this.id.substr(3);
                //            window.location = '/QLTSv1/user/user/detail/UserID/'+id;
                $.ajax({
                    type: "POST",
                    url: "/QLTSv1/asset/upgrade/detail",
                    data: "UpgradeID="+id,
                    success: function(data) {
                        //                        alert(data);
                        var getData = $.parseJSON(data);
                        if(getData.status == 'success'){
                            // get user info success
                            var tag = $("<div title='Upgrade\'s information'></div>");
                            tag.html(
                            '<table><tr><td>Ma tai san</td><td>:</td><td>'+getData.data.MaTS+
                                '</td></tr><tr><td>Ten tai san</td><td>:</td><td>'+getData.data.TenTS+
                                '</td></tr><tr><td>Nguoi su dung</td><td>:</td><td>'+getData.data.Username+
                                '</td></tr><tr><td>Nguoi nang cap</td><td>:</td><td>'+getData.data.Manager+
                                '</td></tr><tr><td>Date</td><td>:</td><td>'+getData.data.Date+
                                '</td></tr><tr><td>Detail</td><td>:</td><td>'+getData.data.Detail+
                                '</td></tr></table>'
                        ).dialog({
                                resizable: false,
                                height:350,
                                width: 250,
                                modal: true,
                                buttons: {
                                    "OK": function() {
                                        $( this ).dialog( "close" );
                                    }
                                }
                            }).dialog("open");
                        }else{
                            // get user info error
                            var tag = $("<div title='Error'></div>");
                            tag.html("<span>"+getData.msg+"</span>").dialog({
                                resizable: false,
                                height:350,
                                width: 450,
                                modal: true,
                                buttons: {
                                    "OK": function() {
                                        $( this ).dialog( "close" );
                                    }
                                }
                            }).dialog("open");
                        }
                    
                    
                    }
                });
            });
        } // detail_record
<?php if ($uRole == 0 || $uRole == 2) : ?>
                    function add_record() {
                        $("#dialog-form").attr('title', 'Create new upgrade');
                        $("#dialog-form").dialog({
                            autoOpen: false,
                            height: 350,
                            width: 400,
                            modal: true,
                            buttons: {
                                "Create an upgrade": function() {
                                    var bValid = true;
                                    allFields.removeClass( "ui-state-error" );
                                    bValid = bValid && checkLength( uname, "username", 3, 16 );
                                    bValid = bValid && checkLength( manager, "manager", 3, 16 );
                            
                                    bValid = bValid && checkRegexp( uname, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
                                    bValid = bValid && checkRegexp( manager, /^[a-z]([0-9a-z_])+$/i, "Manager may consist of a-z, 0-9, underscores, begin with a letter." );
                                    if ( bValid ) {
                                        $.ajax({
                                            type: "POST",
                                            url: "/QLTSv1/asset/upgrade/add",
                                            data: "UpgradeID="+upgradeid.val()+"&MaTS="+maTS.val()+"&Username="+uname.val()+"&Manager="+manager.val()+"&Date="+udate.val()+"&Detail="+udetail.val(),
                                            success: function (data){
                                                var getData = $.parseJSON(data);
                                                var tag = $("<div title='"+getData.status+"'></div>");
                                                tag.html("<span>"+getData.msg+"</span>").dialog({
                                                    resizable: false,
                                                    height:200,
                                                    width: 300,
                                                    modal: true,
                                                    buttons: {
                                                        "OK": function() {
                                                            $('.gridTable').flexReload();
                                                            $( this ).dialog( "close" );
                                                        }
                                                    }
                                                }).dialog("open");
                                            }
                                        });
                                        $( this ).dialog( "close" );      
                                    }
                                                
                                },
                                Cancel: function() {
                                    $( this ).dialog( "close" );
                                }
                            },
                            close: function() {
                                allFields.val( "" ).removeClass( "ui-state-error" );
                            }
                        });
                        $( "#dialog-form" ).dialog( "open" );
                    }
                        
                    function delete_record(command,grid)
                    {
                        var record_count = $('.trSelected',grid).length;
                        if (0 == record_count)
                        {
                            alert('Please select a record first by clicking on the appropriate row.');
                            return;
                        }
                        $('.trSelected',grid).each(function(){
                            var id = this.id.substr(3);
                            $( "#dialog-confirm" ).dialog({
                                resizable: false,
                                height:200,
                                width:500,
                                modal: true,
                                buttons: {
                                    "Delete" : function() {
                                        //                            alert(id);
                                        $.ajax({
                                            type: "POST",
                                            url: '/QLTSv1/asset/upgrade/delete/',
                                            data: 'UpgradeID='+id,
                                            success: function (data){
                                                var getData = $.parseJSON(data);
                                                var tag = $("<div title='"+getData.status+"'></div>");
                                                tag.html("<span>"+getData.msg+"</span>").dialog({
                                                    resizable: false,
                                                    height:200,
                                                    width: 300,
                                                    modal: true,
                                                    buttons: {
                                                        "OK": function() {
                                                            $('.gridTable').flexReload();
                                                            $( this ).dialog( "close" );
                                                        }
                                                    }
                                                }).dialog("open");
                                            }
                                        });
                                        $( this ).dialog( "close" );
                                    },
                                    Cancel: function() {
                                        $( this ).dialog( "close" );
                                    }
                                }
                            });
                        });
                    } // delete_record

                    $( "#Date" ).datepicker({ dateFormat: 'yy-mm-dd' });
                    //////////////////////////////////
                    var tips = $(".validateTips");
                    var upgradeid = $("#UpgradeID"),
                    maTS = $("#MaTS"),
                    uname = $("#Username"),
                    manager = $("#Manager"),
                    udate = $("#Date"),
                    udetail = $("#Detail"),
                    allFields = $( [] ).add(upgradeid).add(maTS).add(uname).add(manager).add(udate).add(udetail);
                                
                    function updateTips( t ) {
                        tips
                        .text( t )
                        .addClass( "ui-state-highlight" );
                        setTimeout(function() {
                            tips.removeClass( "ui-state-highlight", 1500 );
                        }, 500 );
                    }

                    function checkLength( o, n, min, max ) {
                        if ( o.val().length > max || o.val().length < min ) {
                            o.addClass( "ui-state-error" );
                            updateTips( "Length of " + n + " must be between " +
                                min + " and " + max + "." );
                            return false;
                        } else {
                            return true;
                        }
                    }

                    function checkRegexp( o, regexp, n ) {
                        if ( !( regexp.test( o.val() ) ) ) {
                            o.addClass( "ui-state-error" );
                            updateTips( n );
                            return false;
                        } else {
                            return true;
                        }
                    }
<?php endif; ?>
                                        });
                                        -->
</script>